﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Business;
using RM2012Server.Model;

namespace RM2012Server.View
{
    public partial class frmMaterial : Office2007Form
    {
        #region Fields
        MaterialCtrl m_MaterialCtrl = new MaterialCtrl();
        CategoryCtrl m_CategoryCtrl = new CategoryCtrl();
        UnitCtrl m_UnitCtrl = new UnitCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmMaterial()
        {
            InitializeComponent();
        }
        #endregion
        
        #region Load
        private void frmMaterial_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_CategoryCtrl.LoadToComboBox(cmbCate);
            m_CategoryCtrl.LoadToComboBox(a_cmbCategory);
            m_CategoryCtrl.LoadToDataGridViewComboBoxColumn(colCateId);
            m_UnitCtrl.LoadToComboBox(cmbUnit);
            m_UnitCtrl.LoadToComboBox(a_cmbUnit);
            m_UnitCtrl.LoadToDataGridViewComboBoxColumn(colUnitId);
            m_MaterialCtrl.Show(dgvMaterial, bindingNavigatorMaterial, txtMaterialId, txtMaterialName, cmbCate, cmbUnit, txtPrice, chkState, chkIsMenu, txtWarningQuantity, txtStockQuantity, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion
        
        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            try
            {
                double price_temp = Convert.ToDouble(txtPrice.Text);
            }
            catch
            {
                txtPrice.Text = "0.0";
            }
            try
            {
                double warning_temp = Convert.ToDouble(txtWarningQuantity.Text);
            }
            catch
            {
                txtWarningQuantity.Text = "0.0";
            }
            try
            {
                double stock_temp = Convert.ToDouble(txtStockQuantity.Text);
            }
            catch
            {
                txtStockQuantity.Text = "0.0";
            }
            MaterialInfo info = new MaterialInfo(txtMaterialId.Text, txtMaterialName.Text, cmbCate.SelectedValue.ToString(), cmbUnit.SelectedValue.ToString(), Convert.ToDouble(txtPrice.Text), chkState.Checked, chkIsMenu.Checked, Convert.ToDouble(txtWarningQuantity.Text), Convert.ToDouble(txtStockQuantity.Text), txtRemark.Text);
            if (isNew == true)
            {
                m_MaterialCtrl.Add(info);
            } 
            else
            {
                m_MaterialCtrl.Update(info);
            }
            m_MaterialCtrl.Show(dgvMaterial, bindingNavigatorMaterial, txtMaterialId, txtMaterialName, cmbCate, cmbUnit, txtPrice, chkState, chkIsMenu, txtWarningQuantity, txtStockQuantity, txtRemark);
            SaveItem.Enabled = false;
            isNew = false;
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_MaterialCtrl.Show(dgvMaterial, bindingNavigatorMaterial, txtMaterialId, txtMaterialName, cmbCate, cmbUnit, txtPrice, chkState, chkIsMenu, txtWarningQuantity, txtStockQuantity, txtRemark);
            SaveItem.Enabled = false;
        }

        private void btnAddCate_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormCategory();
        }

        private void btnAddUnit_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormUnit();
        }
        #endregion
        
        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvMaterial.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_MaterialCtrl.AddNewRow();
            m_Row["MaterialId"] = GetID.Material(dgvMaterial);
            m_Row["MaterialName"] = "";
            m_Row["CateId"] = "CAT001";
            m_Row["UnitId"] = "UNT001";
            m_Row["Price"] = 0.0;
            m_Row["State"] = false;
            m_Row["IsMenu"] = false;
            m_Row["WarningQuantity"] = 0.0;
            m_Row["StockQuantity"] = 0.0;
            m_Row["Remark"] = "";
            m_MaterialCtrl.AddNewMaterial(m_Row);
            bindingNavigatorMaterial.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_MaterialCtrl.Show(dgvMaterial, bindingNavigatorMaterial, txtMaterialId, txtMaterialName, cmbCate, cmbUnit, txtPrice, chkState, chkIsMenu, txtWarningQuantity, txtStockQuantity, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvMaterial.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvMaterial.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvMaterial.SelectedRows)
                    {
                        bindingNavigatorMaterial.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvMaterial.Rows)
            {
                if (row.Cells["colMaterialId"].Value.ToString() == "" || row.Cells["colMaterialName"].Value.ToString() == "" || row.Cells["colCateId"].Value.ToString() == "" || row.Cells["colUnitId"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_MaterialCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ. Mã và tên nguyên liệu không được để trống !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region DataGridView Event
        private void dgvMaterial_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }

        private void dgvMaterial_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBoxEx.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        #endregion

        #region Searching
        private void rbMaterialId_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Mã nguyên liệu :";
        }

        private void rbMaterialName_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Tên nguyên liệu :";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (rbMaterialId.Checked == true)
                {
                    dt = m_MaterialCtrl.GetByLikeId(txtKeyWord.Text);
                }
                else if (rbMaterialName.Checked)
                {
                    dt = m_MaterialCtrl.GetByName(txtKeyWord.Text);
                }
                else
                {
                    dt = m_MaterialCtrl.GetByName(txtKeyWord.Text);
                }
                m_MaterialCtrl.ShowWithSources(dgvMaterial, bindingNavigatorMaterial, txtMaterialId, txtMaterialName, cmbCate, cmbUnit, txtPrice, chkState, chkIsMenu, txtWarningQuantity, txtStockQuantity, txtRemark, dt);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnTKA_Click(object sender, EventArgs e)
        {
            try
            {
                String materialId = "";
                String materialName = "";
                String cateId = "-1";
                String unitId = "-1";
                bool price = false;
                double minPrice = 0.0;
                double maxPrice = 0.0;

                if (chkMaterialId.Checked == true)
                {
                    materialId = a_txtMenuID.Text;
                }
                if (chkMaterialName.Checked == true)
                {
                    materialName = a_txtMenuName.Text;
                }
                if (chkCateId.Checked == true)
                {
                    cateId = a_cmbCategory.SelectedValue.ToString();
                }
                if (chkUnitId.Checked == true)
                {
                    unitId = a_cmbUnit.SelectedValue.ToString();
                }
                if (chkPrice.Checked == true)
                {
                    price = true;
                    minPrice = Convert.ToDouble(a_txtMin.Text);
                    maxPrice = Convert.ToDouble(a_txtMax.Text);
                }

                DataTable dtSource = m_MaterialCtrl.GetByOption(materialId, materialName, cateId, unitId, price, minPrice, maxPrice);
                m_MaterialCtrl.ShowWithSources(dgvMaterial, bindingNavigatorMaterial, txtMaterialId, txtMaterialName, cmbCate, cmbUnit, txtPrice, chkState, chkIsMenu, txtWarningQuantity, txtStockQuantity, txtRemark, dtSource);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Khoảng giá cần nhập đầy đủ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region Context Menu Event Click
        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
